﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum PlayerState
{
    [Header("地面")]
    Ground = 0, //地面
    [Header("跳跃上升")]
    Jump = 1, //跳跃上升
    [Header("下降")]
    Fall = 2, //下降
    [Header("贴墙下滑")]
    Climb = 3, //贴墙下滑
}
public class Player_LeLe : MonoBehaviour
{
    public Rigidbody2D rigidbody2D;
    public Collider2D collider2D;
    public Animator animator;

    [Header("移动速度")]
    public float moveSpeed = 8f;
    [Header("爬墙摩擦下落速度")]
    public float climbFrictionSpeed = 1f;
    [Header("地面纵向起跳力")]
    public float jumpForce = 14f;
    [Header("墙面纵向起跳力")]
    public float climbJumpForce = 10f;
    [Header("墙面横向反弹力")]
    public float climbFantanSpeed = 6f;
    
    
    [Header("哪些layer层算地面")]
    public LayerMask whatIsGround;
    [Header("玩家的脚检测点")]
    public Transform feetPos;
    [Header("玩家的脚检测半径")]
    public float checkRadius;

    [Header("玩家的眼前方检测点")]
    public Transform handPos;
    [Header("玩家的眼前方检测半径")]
    public float handLength = 0.2f;

    [Header("玩家的左右朝向 x:+1代表向右,x:-1代表向左")]
    public Vector2 direction;

    [Header("玩家当前状态")]
    public PlayerState playerState;

    [Header("是否按下跳跃按键")]
    public bool jumpPressed = false;


    public GameObject prefab_PlayerDash;
    public float dashTimer = 0;
    public float dashInterval = 0.5f;

    void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        animator = GetComponent<Animator>();
        
        playerState = PlayerState.Ground;
        direction = Vector2.right;
        SetFace();
    }

    public void Init()
    {
        //rigidbody2D.bodyType = RigidbodyType2D.Static;
    }

    void Update()
    {
        if (Input.GetButtonDown("Jump") && (playerState == PlayerState.Ground || playerState == PlayerState.Climb))
        {
            jumpPressed = true;
        }
    }
    
    void FixedUpdate()
    {
        GroundMovement();


        if (rigidbody2D.velocity.y <= 0 && playerState != PlayerState.Climb)
        {
            playerState = PlayerState.Fall;
        }

        //是否靠紧墙
        bool isCloseToWall = false;
        RaycastHit2D[] hits_hand = ShuaiFunctions.RaycastAll(handPos.position, Vector2.zero, direction, handLength, whatIsGround, Color.red);
        if (hits_hand.Length >= 1)
        {
            isCloseToWall = true;
        }


        if (isCloseToWall == true && playerState == PlayerState.Fall && Mathf.Abs(rigidbody2D.velocity.x) > 0)
        {
            //Debug.Log(hits_hand[0]);
            playerState = PlayerState.Climb;
        }
        else if (isCloseToWall == false && playerState == PlayerState.Climb)
        {
            playerState = PlayerState.Fall;
        }

        //脚底是否检测到地面
        Collider2D coll = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);
        if (coll != null)
        {
            playerState = PlayerState.Ground;
        }

        if (playerState == PlayerState.Climb)
        {
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, -climbFrictionSpeed);
        }


        
        Jump();
        Stamp();


        SwitchAnimation();
    }

    //踩怪攻击
    public void Stamp()
    {
        Collider2D c = Physics2D.OverlapCircle(feetPos.position, 0.1f, LayerMask.GetMask("Enemy"));
        if (c == null)
        {
            return;
        }

        Opossum opossum = c.gameObject.GetComponent<Opossum>();
        if (opossum != null)
        {
            opossum.Dead();
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, 0);
            rigidbody2D.AddForce(new Vector2(0, 500f));
        }
    }

    //横向移动
    public void GroundMovement()
    {
        float horizontalMove = Input.GetAxisRaw("Horizontal");
        rigidbody2D.velocity = new Vector2(horizontalMove * moveSpeed, rigidbody2D.velocity.y);

        if (rigidbody2D.velocity.x != 0)
        {
            direction = new Vector2(rigidbody2D.velocity.x / Mathf.Abs(rigidbody2D.velocity.x), 0);
            SetFace();

            dashTimer += Time.fixedDeltaTime;
            if (dashTimer >= dashInterval)
            {
                dashTimer = 0f;
                GameObject.Instantiate(prefab_PlayerDash, transform.position, Quaternion.identity);
            }
            
        }
    }

    //跳跃
    public void Jump()
    {
        //if (playerState == PlayerState.Ground)
        //{
        //    playerState = PlayerState.Jump;
        //}
        if (jumpPressed && playerState == PlayerState.Ground)
        {
            jumpPressed = false;
            playerState = PlayerState.Jump;
            rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, jumpForce);
        }
        if (jumpPressed && playerState == PlayerState.Climb)
        {
            jumpPressed = false;
            playerState = PlayerState.Jump;
            rigidbody2D.velocity = new Vector2(-direction.x * climbFantanSpeed, climbJumpForce);
            //rb.AddForce(new Vector2(-direction.x * climbFantanSpeed, climbJumpForce), ForceMode2D.Force);
        }
    }

    public void SwitchAnimation()
    {
        animator.SetFloat("x_speed", Mathf.Abs(rigidbody2D.velocity.x));

        if (rigidbody2D.velocity.y != 0)
        {
            animator.SetFloat("y_speed", rigidbody2D.velocity.y);
        }

        animator.SetInteger("playerState", (int)playerState);
    }

    public void SetFace()
    {
        Vector3 theScale = transform.localScale;
        theScale.x = direction.x * Mathf.Abs(theScale.x);
        transform.localScale = theScale;
    }
    
    public void OnDrawGizmos()
    {
        if (playerState == PlayerState.Ground)
        {
            Gizmos.color = Color.red;
        }
        else
        {
            Gizmos.color = Color.green;
        }

        Vector2 boxCenter = (Vector2)transform.position + (Vector2.down * 1 * 0.5f);
        Gizmos.DrawWireCube(boxCenter, new Vector2(0.8f, 0.5f));
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            Debug.Log("游戏结束");
            ShuaiManager.Instance.EnterLevel();
        }
    }
}
